package com.ahs.support;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @Author JinX
 * @Date 2021/3/31 16:10
 * @Version 1.0
 */
public class Generator {

    public static void main(String[] args) {
        TemplateConfig templateConfig = new TemplateConfig()
                .setServiceImpl("templates/serviceImpl.java")
                .setController("templates/controller.java")
                .setXml("templates/mapper.xml")
                .setService("templates/service.java")
                .setEntity("templates/entity.java");
        // 配置模板引擎
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setTemplate(templateConfig);

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("riven zhang");
        gc.setOpen(false);
        gc.setFileOverride(true);
        gc.setServiceName("%sService");
        gc.setDateType(DateType.ONLY_DATE);
//        / 实体属性 Swagger2 注解
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        /// 配置模块
        // pc.setModuleName("health_7");
        pc.setParent("com.ahs");
        // 默认是entity
        pc.setEntity("pojo");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
//        strategy.setInclude("t_member", "t_order_setting", "t_setmeal", "t_order");
        strategy.setTablePrefix("t_");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        /// 是否有逻辑删除
        //strategy.setLogicDeleteFieldName("deleted");

        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
